﻿/*
Copyright 2010 CumulusCloud
http://cumuluscloud.cc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using Microsoft.Win32;

namespace windows_tray
{
    public partial class Principal : Form
    {
        public Principal()
        {
            InitializeComponent();
        }

        private void Principal_Load(object sender, EventArgs e)
        {
            UpdateStatus();
        }

        private void UpdateStatus()
        {
            service.Refresh();

            if (service.Status == ServiceControllerStatus.Running)
            {
                notifyStopped.Visible = false;
                notifyStarted.Visible = true;
            }
            else
            {
                notifyStarted.Visible = false;
                notifyStopped.Visible = true;
            }
        }

        private void tmrUpdate_Tick(object sender, EventArgs e)
        {
            UpdateStatus();
        }

        private void Quit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void StartService_Click(object sender, EventArgs e)
        {
            try
            {
                service.Start();
            }
            catch
            {
                ProcessStartInfo processInfo = new ProcessStartInfo();
                processInfo.Verb = "runas";
                processInfo.FileName = "net";
                processInfo.Arguments = "start cumuluscloud";
                try
                {
                    Process.Start(processInfo);
                }
                catch
                {
                }
            }
        }

        private void StopService_Click(object sender, EventArgs e)
        {
            try
            {
                service.Stop();
            }
            catch
            {
                ProcessStartInfo processInfo = new ProcessStartInfo();
                processInfo.Verb = "runas";
                processInfo.FileName = "net";
                processInfo.Arguments = "stop cumuluscloud";
                try
                {
                    Process.Start(processInfo);
                }
                catch
                {
                }
            }
        }

        private void RestartService_Click(object sender, EventArgs e)
        {
            StopService_Click(null, null);
            Thread.Sleep(5000);
            StartService_Click(null, null);
        }

        private void openNavigator(object sender, EventArgs e)
        {
            // Get the server.xml file
            RegistryKey cumulusKey = Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\CumulusCloud");
            String serverLocation = (String) cumulusKey.GetValue("InstallLocation");
            serverLocation = serverLocation.Trim('"');
            serverLocation += "\\conf\\server.xml";

            // Get the port
            XPathDocument doc = new XPathDocument(serverLocation);
            XPathNavigator nav = doc.CreateNavigator();
            XPathNodeIterator iter = nav.Select("/Server/Service/Connector/@port");

            iter.MoveNext();
            String port = iter.Current.Value;

            Process.Start("http://127.0.0.1:" + port);
        }

        private void checkUpdate(object sender, EventArgs e)
        {
            // Get the installed version
            RegistryKey cumulusKey = Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\CumulusCloud");
            String installedVersion = (String)cumulusKey.GetValue("DisplayVersion");

            // Get the current available version
            WebRequest request = WebRequest.Create("http://cumuluscloud.cc/currver.txt");
            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream);
            String availableVersion = sr.ReadLine();
            sr.Close();

            // Compare the two
            if (availableVersion.Equals(installedVersion))
            {
                MessageBox.Show("You already have the most updated version: " + availableVersion);
            }
            else
            {
                MessageBox.Show("You currently have " + installedVersion + " and there is a new version " + availableVersion + ". Press OK to download it");
                Process.Start("http://cumuluscloud.googlecode.com/files/CumulusCloud-" + availableVersion + ".exe");
            }
        }
    }
}
